package Avanzado.net;

import java.net.*;
import java.io.*;

public class SimpleClient
{
	private Socket _socket = null;
	
	private OutputStreamWriter _out = null;
	
	public SimpleClient(String serverAddr, int serverPort) throws IOException
	{
		this.setupConnection(serverAddr, serverPort);
	}
	
	private void setupConnection(String serverAddr, int serverPort) throws IOException
	{
		_socket = new Socket();
		_socket.connect(new InetSocketAddress(serverAddr, serverPort));		
		_out = new OutputStreamWriter(_socket.getOutputStream());
	}
	
	public void close()
	{
		if (_out != null)
		{
			try
			{
				_out.close();
			}
			catch (IOException ex2)
			{
				ex2.printStackTrace();
			}
		}
		
		if (_socket != null)
		{
			try
			{
				_socket.close();
			}
			catch (IOException ex2)
			{
				ex2.printStackTrace();
			}
		}
	}
	
	public void sendMessage(String message) throws IOException
	{
		_out.write(message);
		_out.flush();
	}
	
}
